package org.qy.star_server.data.center;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.qy.star_common.enums.ExchangeType;
import org.qy.star_common.message.Message;
import org.qy.star_server.core.*;
import org.qy.star_server.data.manager.DBManager;
import org.qy.star_server.data.manager.FileManager;

import org.qy.star_server.data.mapper.MetaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author yinger
 * @description 操作磁盘数据
 * @date 2024/3/28 16:51
 **/
@Slf4j
@Data
public class DiskDataCenter {

    public DiskDataCenter(MetaMapper metaMapper) {
        fileManager = new FileManager();
        dbManager = new DBManager(metaMapper);

    }

    FileManager fileManager;
    DBManager dbManager;

    public void createQueueFiles(String username, String virtualHostName, String queueName) {
        fileManager.createQueueFiles(Queue.getQueryQueue(username, virtualHostName, queueName));
    }

    public void deleteQueueFiles(String username, String virtualHostName, String queueName) {
        fileManager.deleteQueueFiles(Queue.getQueryQueue(username, virtualHostName, queueName));
    }


    public void insertMessage(String username, String virtualHostName, String queueName, Message message) {
        fileManager.insertMessage(MemoryDataCenter.memory.selectQueue(username, virtualHostName, queueName), message);
    }

    public void deleteMessage(String username, String virtualHostName, String queueName, Message message) {
        fileManager.deleteMessage(MemoryDataCenter.memory.selectQueue(username, virtualHostName, queueName), message);
    }

    public void performGarbageCollection(String username, String virtualHostName, String queueName) throws IOException, InterruptedException {
        Queue queue = Queue.getQueryQueue(username, virtualHostName, queueName);
        if (fileManager.checkGC(queue)) {
            fileManager.gc(queue);
        }
    }


    public void createTables() {
        if (!dbManager.createUserTable()) {
            log.error("创建用户表失败");
        }
        if (!dbManager.createVirtualHostTable()) {
            log.error("创建虚拟主机表失败");
        }
        if (!dbManager.createExchangeTable()) {
            log.error("创建交换机表失败");
        }
        if (!dbManager.createQueueTable()) {
            log.error("创建队列表失败");
        }
        if (!dbManager.createBindingTable()) {
            log.error("创建绑定表失败");
        }
    }

    public boolean insertUser(String username, String password) {
        return dbManager.insertUser(new User(username, password));
    }

    public boolean deleteUser(String username) {
        return dbManager.deleteUser(username);
    }

    public boolean updateUser(String username, String newPassword) {
        return dbManager.updateUser(new User(username, newPassword));
    }

    public User selectUser(String username) {
        return dbManager.selectUser(username);
    }

    public List<User> selectAllUser() {
        return dbManager.selectAllUser();
    }

    public boolean insertVirtualHost(String username, String virtualHostName) {
        return dbManager.insertVirtualHost(new VirtualHost(username, virtualHostName));
    }

    public boolean deleteVirtualHost(String username, String virtualHostName) {
        return dbManager.deleteVirtualHost(new VirtualHost(username, virtualHostName));
    }

    public VirtualHost selectVirtualHost(String username, String virtualHostName) {
        return dbManager.selectVirtualHost(new VirtualHost(username, virtualHostName));
    }

    public List<VirtualHost> selectAllVirtualHost() {
        return dbManager.selectAllVirtualHost();
    }

    public boolean insertExchange(String username, String virtualHostName, String exchangeName, ExchangeType exchangeType, boolean durable, boolean autoDelete, Map<String, Object> arguments) {
        return dbManager.insertExchange(Exchange.getInsertExchange(username, virtualHostName, exchangeName, exchangeType, durable, autoDelete, arguments));
    }

    public boolean deleteExchange(String username, String virtualHostName, String exchangeName) {
        return dbManager.deleteExchange(Exchange.getQueryExchange(username, virtualHostName, exchangeName));
    }

    public Exchange selectExchange(String username, String virtualHostName, String exchangeName) {
        return dbManager.selectExchange(Exchange.getQueryExchange(username, virtualHostName, exchangeName));
    }

    public List<Exchange> selectAllExchange() {
        return dbManager.selectAllExchange();
    }


    public boolean insertQueue(String username, String virtualHostName, String queueName, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) {
        if (dbManager.insertQueue(new Queue(username, virtualHostName, queueName, durable, exclusive, autoDelete, arguments))) {
            createQueueFiles(username, virtualHostName, queueName);
            return true;
        }
        return false;
    }

    public boolean deleteQueue(String username, String virtualHostName, String queueName) {
        if (dbManager.deleteQueue(Queue.getQueryQueue(username, virtualHostName, queueName))) {
            deleteQueueFiles(username, virtualHostName, queueName);
            return true;
        }
        return false;
    }

    public Queue selectQueue(String username, String virtualHostName, String queueName) {
        return dbManager.selectQueue(new Queue(username, virtualHostName, queueName, false, false, false, null));
    }

    public List<Queue> selectAllQueue() {
        return dbManager.selectAllQueue();
    }

    public boolean insertBinding(String username, String virtualHostName, String exchangeName, String queueName, String bindingKey) {
        return dbManager.insertBinding(Binding.getInsertBinding(username, virtualHostName, exchangeName, queueName, bindingKey));
    }

    public boolean deleteBinding(String username, String virtualHostName, String exchangeName, String queueName) {
        return dbManager.deleteBinding(Binding.getQueryBinding(username, virtualHostName, exchangeName, queueName));
    }

    public Binding selectBinding(String username, String virtualHostName, String exchangeName, String queueName) {
        return dbManager.selectBinding(Binding.getQueryBinding(username, virtualHostName, exchangeName, queueName));
    }

    public List<Binding> selectAllBinding() {
        return dbManager.selectAllBinding();
    }


}
